<template>
  <div>
    <my-breadcrumb :items="['商家大全', '二手商城']"/>
    <search :formItems="searchItems" :check="true" @search="getList" ref="search"/>
    <div class="add">
      <el-button type="primary" @click="toAdd">新增闲置</el-button>
      <el-button type="primary" @click="toExport" :loading="loading">导出闲置</el-button>
    </div>
    <el-table :data="shops" border @sort-change="orderByTime">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        label="ID"
        prop="id">
      </el-table-column>
      <el-table-column
        label="创建时间"
        :show-overflow-tooltip="true"
        sortable="custom"
        width="100px"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="闲置属性"
        prop="addtype">
        <template slot-scope="scope">
          {{ scope.row.addtype === '1' ? '闲置' : '求购' }}
        </template>
      </el-table-column>
      <el-table-column
        label="标题"
        :show-overflow-tooltip="true"
        prop="title">
      </el-table-column>
      <el-table-column
        label="商品名称"
        prop="skirtname">
      </el-table-column>
      <el-table-column
        label="发布人"
        prop="membername">
      </el-table-column>
      <el-table-column
        label="店铺名称"
        :show-overflow-tooltip="true"
        prop="businessname">
      </el-table-column>
      <el-table-column
        label="发布开始时间"
        sortable="custom"
        width="130px"
        :show-overflow-tooltip="true"
        prop="starttime">
      </el-table-column>
      <el-table-column
        label="发布结束时间"
        sortable="custom"
        width="130px"
        :show-overflow-tooltip="true"
        prop="endtime">
      </el-table-column>
      <el-table-column
        label="价格"
        sortable="custom"
        prop="price">
      </el-table-column>
      <el-table-column
        label="价格区间">
        <template slot-scope="scope">
          {{ scope.row.mixprice }} - {{ scope.row.maxprice }}
        </template>
      </el-table-column>
      <el-table-column
        label="是否急售"
        sortable="custom"
        width="110px"
        prop="type">
        <template slot-scope="scope">
          {{ scope.row.type === '1' ? '' : (scope.row.addtype === '1' ? '急售' : '急求') }}
        </template>
      </el-table-column>
      <el-table-column
        label="删除原因"
        prop="delcontent">
      </el-table-column>
      <el-table-column
        label="操作"
        width="100px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row)" :disabled="scope.row.delflag === '1'">编辑</el-button>
          <el-button v-if="scope.row.delflag === '0'" size="mini" type="text" @click="toDelete(scope.row.id, scope.row.memberid)">删除</el-button>
          <el-button v-else size="mini" type="text" disabled>已删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog :title="isAdd ? '新增记录' : '信息修改'" :visible.sync="showAdd">
      <el-form :model="shop" :rules="rules" inline label-width="100px" ref="addForm">
        <el-form-item label="发布类型" prop="addtype">
          <el-select v-model="shop.addtype">
            <el-option value="1" label="出售"/>
            <el-option value="2" label="求购"/>
          </el-select>
        </el-form-item>
        <el-form-item label="预约编号" prop="limitid">
          <el-date-picker
            v-model="shop.limitid"
            value-format="yyyy-MM-dd"
            type="date"
            :picker-options="pickerOptions"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="裙子名称" prop="skirtid">
          <div class="select-box">
            <img :src="nowSkirt.img1" alt="" width="40" v-if="nowSkirt.img1">
            <span v-if="nowSkirt.name" style="margin: 0 16px;">{{ nowSkirt.name }}</span>
            <el-button type="text" @click="toSelectSkirt">点击选择裙子</el-button>
          </div>
        </el-form-item>
        <el-form-item label="商家名称" prop="businessid">
          <span style="display: inline-block;width: 500px;">{{ nowSkirt.membername ? nowSkirt.membername : '请先选择裙子' }}</span>
        </el-form-item>
        <el-form-item label="用户" prop="memberid" style="width: 500px;">
          <div class="select-box">
            <img :src="nowUser.avatar" alt="" width="40" v-show="nowUser.avatar">
            <span v-show="nowUser.name" style="margin: 0 20px 0 12px;">{{ nowUser.name }}</span>
            <el-button type="text" @click="toSelectUser">{{ nowUser.name ? '重新选择用户' : '选择用户' }}</el-button>
          </div>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="shop.title"/>
        </el-form-item>
        <el-form-item label="新旧程度" prop="oandnid" v-if="shop.addtype === '1'">
          <el-select v-model="shop.oandnid">
            <el-option v-for="(item, index) in old" :key="index" :value="item.id" :label="item.name"/>
          </el-select>
        </el-form-item>
        <el-form-item label="新旧程度" prop="oandnid" v-if="shop.addtype === '2'">
          <el-select v-model="shop.oandnid">
            <el-option value="1" label="非全新"/>
            <el-option value="10" label="全新"/>
          </el-select>
        </el-form-item>
        <el-form-item label="闲置价格" prop="price" v-if="shop.addtype === '1'">
          <el-input v-model="shop.price"/>
        </el-form-item>
        <el-form-item label="淘宝口令" prop="tbtoken">
          <el-input v-model="shop.tbtoken" type="textarea" style="width: 500px;"/>
        </el-form-item>
        <el-form-item :label="shop.addtype === '1' ? '是否急售' : '是否急求'" prop="type">
          <el-select v-model="shop.type">
            <el-option value="2" label="是"/>
            <el-option value="1" label="否"/>
          </el-select>
        </el-form-item>
        <el-form-item label="详情" prop="content">
          <el-input v-model="shop.content" type="textarea" style="width: 500px;"/>
        </el-form-item>
        <el-form-item label="配件" prop="parts">
          <el-input v-model="shop.parts"/>
        </el-form-item>
        <el-form-item label="颜色" prop="colour" v-if="shop.addtype === '1'">
          <el-input v-model="shop.colour"/>
        </el-form-item>
        <el-form-item label="尺码" prop="clothessize" v-if="shop.addtype === '1'">
          <el-input v-model="shop.clothessize"/>
        </el-form-item>
        <el-form-item label="求购最低价" prop="mixprice" v-if="shop.addtype === '2'">
          <el-input v-model="shop.mixprice"/>
        </el-form-item>
        <el-form-item label="求购最高价" prop="maxprice" v-if="shop.addtype === '2'">
          <el-input v-model="shop.maxprice"/>
        </el-form-item>
        <el-form-item label="图片" prop="imgarr" style="width: 100%;">
          <!-- 图片列表 -->
          <div class="img-box" v-for="(image, index) in imageArr" :key="index">
            <img :src="image" class="avatar">
            <i class="el-icon-close" @click="delImage(index)"></i>
          </div>
          <el-upload
            class="avatar-uploader"
            action="#"
            v-if="imageArr.length < 6"
            :show-file-list="false"
            :before-upload="beforeUpload">
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showAdd = false">取消</el-button>
      </div>
    </el-dialog>
    <select-list ref="selectList" :url="'/Skirti/index?status=0&delflag=0'" :searchItems="skirtSearch" :tableItem="skirtTableItems" @selectRow="skirtChange"/>
    <select-list ref="selectListUser" :url="'/Member/index?delflag=0'" :searchItems="userSearch" :tableItem="userTable" @selectRow="selectUser"/>
  </div>
</template>

<script>
import {deletePrompt, toExport} from '@/utils/getList'
import CLAZZ from '@/config/classify'
import tableItems from '@/config/tableItems'
import selectList from '@/components/common/selectList'
export default {
  name: 'shop',
  data () {
    return {
      searchItems: [
        {
          label: '标题',
          type: 'input',
          key: 'title'
        },
        {
          label: '商品名称',
          type: 'input',
          key: 'skirtname'
        },
        {
          label: '发布人',
          type: 'input',
          key: 'membername'
        },
        {
          label: '店铺名称',
          type: 'input',
          key: 'businessname'
        },
        {
          label: '发布时间',
          type: 'times',
          key: 'times'
        },
        {
          label: '闲置类型',
          type: 'select',
          key: 'addtype',
          options: [{id: '1', name: '出售'}, {id: '2', name: '求购'}]
        },
        {
          label: '是否急售',
          type: 'select',
          key: 'type',
          options: [{id: '1', name: '否'}, {id: '2', name: '是'}]
        },
        {
          label: '最低价格',
          type: 'input',
          key: 'priceMin'
        },
        {
          label: '最高价格',
          type: 'input',
          key: 'priceMax'
        }
      ], // 检索结构
      skirtSearch: [
        {
          label: '裙子名称',
          type: 'input',
          key: 'skirtname'
        },
        {
          label: '商家名称',
          type: 'input',
          key: 'businessname'
        }
      ], // 检索结构
      userSearch: [
        {
          label: '用户名称',
          type: 'input',
          key: 'membername'
        },
        {
          label: '手机号',
          type: 'input',
          key: 'phone'
        },
        {
          label: '性别',
          type: 'select',
          key: 'sex',
          options: [{id: '1', name: '男'}, {id: '2', name: '女'}, {id: '3', name: '未知'}, {id: '4', name: '保密'}]
        },
        {
          label: '风格',
          type: 'select',
          key: 'styleid',
          options: CLAZZ.style
        },
        {
          label: '爱好程度',
          type: 'select',
          key: 'degreeid',
          options: CLAZZ.degreeid
        },
        {
          label: '地区',
          type: 'address',
          key: 'address'
        },
        {
          label: '拥有裙子数',
          type: 'input',
          key: 'skirtnum'
        },
        {
          label: '等级',
          type: 'select',
          key: 'level',
          options: [{id: '1', name: '一级'}, {id: '2', name: '二级'}, {id: '3', name: '三级'}]
        }
      ],
      skirtTableItems: tableItems.skirtList, // 选择裙子表格结构
      userTable: tableItems.userList,
      shops: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      imageArr: [],
      shop: {
        memberid: '',
        addtype: '',
        limitid: '',
        skirtid: '',
        skirtname: '',
        businessid: '',
        businessname: '',
        title: '',
        oandnid: '',
        price: '',
        tbtoken: '',
        type: '',
        content: '',
        parts: '',
        colour: '',
        clothessize: '',
        mixprice: '',
        maxprice: '',
        imgarr: []
      },
      rules: {
        addtype: [
          { required: true, message: '请选择发布类型', trigger: 'change' }
        ],
        limitid: [
          { required: true, message: '请填写预约编号', trigger: 'change' }
        ],
        skirtid: [
          { required: true, message: '请选择裙子', trigger: 'change' }
        ],
        // skirtname: [
        //   { required: true, message: '', trigger: 'change' }
        // ],
        businessid: [
          { required: true, message: '请选择商家', trigger: 'change' }
        ],
        memberid: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ],
        // businessname: [
        //   { required: true, message: '', trigger: 'change' }
        // ],
        title: [
          { required: true, message: '请输入标题', trigger: 'change' }
        ],
        oandnid: [
          { required: true, message: '请选择新旧程度', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'change' }
        ],
        // tbtoken: [
        //   { required: true, message: '', trigger: 'change' }
        // ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入详情', trigger: 'change' }
        ],
        // parts: [
        //   { required: true, message: '', trigger: 'change' }
        // ],
        // colour: [
        //   { required: true, message: '', trigger: 'change' }
        // ],
        // clothessize: [
        //   { required: true, message: '', trigger: 'change' }
        // ],
        mixprice: [
          { required: true, message: '请输入最低价', trigger: 'change' }
        ],
        maxprice: [
          { required: true, message: '请输入最高价', trigger: 'change' }
        ],
        imgarr: [
          { required: true, message: '请选择图片', trigger: 'change' }
        ],
        tbtoken: [
          { required: true, message: '请填写淘口令', trigger: 'blur' }
        ]
      },
      isAdd: true,
      showAdd: false,
      old: CLAZZ.oandnid,
      updateRow: null,
      loading: false,
      lifting: null,
      nowSkirt: {},
      nowUser: {},
      pickerOptions: {
        disabledDate (date) {
          return (date.valueOf() < (new Date().valueOf() - 3600 * 1000 * 24) || date.valueOf() > (new Date().valueOf() + 3600 * 1000 * 15 * 24))
        }
      }
    }
  },
  methods: {
    getList () {
      let searchData = {delflag: '0'}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
        if (searchData.priceMin && searchData.priceMax) {
          searchData.price = searchData.priceMin + ' and ' + searchData.priceMax
        } else if (searchData.priceMin && !searchData.priceMax) {
          searchData.price = searchData.priceMin + ' and 9999999999'
        } else if (!searchData.priceMin && searchData.priceMax) {
          searchData.price = '0 and ' + searchData.priceMax
        }
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize, orderby: this.lifting}
      this.axios.get('/Used/index', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.shops = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },

    toAdd () {
      this.isAdd = true
      this.showAdd = true
      this.nowSkirt = {}
      this.nowUser = {}
      this.imageArr = []
      this.$nextTick(() => {
        this.$refs['addForm'].resetFields()
        Object.keys(this.shop).forEach(key => {
          this.shop[key] = ''
        })
      })
    },
    toUpdate (row) {
      this.isAdd = false
      this.showAdd = true
      this.updateRow = row
      this.nowSkirt = {
        name: row.skirtname,
        membername: row.businessname
      }
      this.nowUser.name = row.membername
      let tempArr = []
      this.$nextTick(() => {
        Object.keys(this.shop).forEach(key => {
          this.shop[key] = row[key]
        })
        Object.keys(row).forEach(key => {
          if (key.indexOf('img') !== -1 && row[key]) {
            tempArr.push(row[key])
          }
        })
        this.shop.imgarr = JSON.parse(JSON.stringify(tempArr))
        this.imageArr = JSON.parse(JSON.stringify(tempArr))
        this.shop.limitid = row.starttime.split(' ')[0]
        this.$refs['addForm'].clearValidate()
      })
    },
    toSubmit () {
      let url = '/used/add'
      let params = {}
      Object.keys(this.shop).forEach(key => {
        params[key] = this.shop[key]
      })
      if (!this.isAdd) {
        url = '/used/edit'
        params.id = this.updateRow.id
      }
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.axios.post(url, this.qs.stringify(params))
            .then(res => {
              console.log(res)
              if (res.data.status === 1) {
                this.$message.success(res.data.msg)
                this.showAdd = false
                this.getList()
              }
            })
        } else {
          this.$message.error('请检查表单是否填写正确')
          return false
        }
      })
    },
    toDelete (id, memberId) {
      deletePrompt({url: '/Used/delete', id, memberId}, res => {
        if (res.data.status === 1) {
          this.$message.success(res.data.msg)
          this.getList()
        }
      })
    },

    toExport () {
      this.loading = true
      let searchData = this.$refs['search'].search()
      toExport('/Used/excelindex', searchData, '闲置', () => {
        this.loading = false
      })
    },
    orderByTime (lifting) {
      if (lifting.order === 'ascending') {
        this.lifting = lifting.prop + ' asc'
      } else {
        this.lifting = lifting.prop + ' desc'
      }
      this.getList()
    },

    beforeUpload (file) {
      let reader = new FileReader()
      let _self = this
      reader.onload = function (ev) {
        _self.imageArr.push(ev.currentTarget.result)
        _self.shop.imgarr.push(ev.currentTarget.result)
      }
      reader.readAsDataURL(file)
      return false
    },
    delImage (index) {
      this.imageArr.splice(index, 1)
      this.detailRow.imgarr.splice(index, 1)
    },

    toSelectSkirt () {
      this.$refs['selectList'].toShow()
    },
    skirtChange (row) {
      this.nowSkirt = row
      this.shop.skirtid = row.id
      this.shop.skirtname = row.name
      this.shop.businessid = row.memberid
      this.shop.businessname = row.membername
    },
    toSelectUser () {
      this.$refs['selectListUser'].toShow()
    },
    selectUser (row) {
      this.nowUser = row
      this.shop.memberid = row.id
    }
  },
  components: {
    selectList
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>
  .option-slot{
    display: flex;
    width: 100%;
    justify-content: center;
  }
  .option-slot .name2{
    font-size: 10px;
    color: #666;
    flex-grow: 0;
  }
  .option-slot .name1{
    flex-grow: 1;
    font-size: 13px;
  }
  .el-select-dropdown__item{
    height: auto;
  }
  .avatar-uploader{
    display: inline-block;
    overflow: hidden;
  }
  .avatar-uploader-icon:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .img-box{
    display: inline-block;
    position: relative;
  }
  .img-box i{
    position: absolute;
    top: 10px;
    right: 10px;
  }
  .avatar {
    width: 100px;
    height: 100px;
    margin-right: 10px;
    border-radius: 6px;
    overflow: hidden;
  }
</style>
